package com.kgcx.system.service;

import com.kgcx.common.constant.Cons;
import com.kgcx.common.utils.CollectionUtils;
import com.kgcx.system.domain.AppGuide;
import com.kgcx.system.domain.AppGuidePage;
import com.kgcx.system.mapper.AppGuideMapper;
import com.kgcx.system.mapper.AppGuidePageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 吴刚林
 * @date 2017/11/20
 */
@Service
public class AppGuideService {
    @Autowired
    private AppGuideMapper appGuideMapper;

    @Autowired
    private AppGuidePageMapper appGuidePageMapper;


    public AppGuide getAppGuide(String version) {
        AppGuide appGuide = new AppGuide();
        appGuide.setVersion(version);
        appGuide = appGuideMapper.selectOneActive(appGuide);
        if (appGuide != null) {
            AppGuidePage appGuidePage = new AppGuidePage();
            appGuidePage.setAppGuideId(appGuide.getKid());
            List<AppGuidePage> appGuidePageList = appGuidePageMapper.selectActive(appGuidePage);
            appGuide.setAppGuidePageList(appGuidePageList);
            return appGuide;
        }
        return null;

    }

    @Transactional(rollbackFor = Exception.class)
    public void addAppGuide(AppGuide appGuide) {
        appGuideMapper.insertSelective(appGuide);
        List<AppGuidePage> appGuidePageList = appGuide.getAppGuidePageList();
        if (CollectionUtils.isNotEmpty(appGuidePageList)) {
            appGuidePageList.forEach(i -> appGuidePageMapper.insertSelective(i));
        }

    }

    @Transactional(rollbackFor = Exception.class)
    public void updateAppGuide(AppGuide appGuide) {
        appGuideMapper.updateByPrimaryKeySelective(appGuide);

    }

    @Transactional(rollbackFor = Exception.class)
    public void removeAppGuide(Long kid) {
        AppGuide appGuide = new AppGuide();
        appGuide.setKid(kid);
        appGuide.setStatus(Cons.COMMON_STATUS_DELETE);
        appGuideMapper.updateByPrimaryKeySelective(appGuide);

    }
}
